package com.zhledu.datastructure.List;

import java.text.NumberFormat;
import java.util.Scanner;

public class MyLinkedList {
    private static final Integer MaxSize = 20;

    public static boolean initList(Node<Number> list) {
        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入列表初始的长度：0到" + (MaxSize - 1));
        int len = scanner.nextInt();
        if (len < 0 || len > MaxSize - 1) {
            System.out.println("您输入的数据有误");
            return false;
        }
        list.data = null;
        list.next = new Node();
        Node node = list.next;
        for (int i = 0; i < len - 1; i++) {
            node.data = (int) (Math.random() * 100) + 1;
            node.next = new Node();
            node = node.next;
        }
        node.data = (int) (Math.random() * 100) + 1;
        node.next = null;
        return true;
    }

    public static void print(Node<Number> list) {
        System.out.println("遍历列表");
        Node node = list;
        while (node.next != null) {
            node = node.next;
            System.out.print(node.data + "\t");
        }
        System.out.println();
    }

    public int length(Node<Number> list) {
        int len = 0;
        Node node = list;
        while (node.next != null) {
            node = node.next;
            len++;
        }
        return len;
    }

    public static boolean insert(Node<Number> list, int index, Number elem) {
        if (index < 0) {
            System.out.println("插入的位置错误");
            return false;
        } else {
            Node node = list;
            Node insertNode = new Node();
            insertNode.data = elem;
            int i = 0;
            boolean inserted = false;
            while (node.next != null) {
                if (i == index) {
                    insertNode.next = node.next;
                    node.next = insertNode;
                    inserted = true;
                } else {
                    i++;
                    node = node.next;
                }
            }
            if (!inserted) {
                System.out.println("输入的插入点太大，将在最后插入");
                node.next = insertNode;
                insertNode.next = null;
            }
            return true;
        }
    }

    public static int find(Node<Number> list, Number elem) {
        int i = 0;
        Node node = list;
        while (node.next != null) {
            node = node.next;
            i++;
            if (node.data.doubleValue() == elem.doubleValue()) {
                return i;
            }
        }
        return -1;
    }

    public static Number get(Node<Number> list, int index) {
        int i = 0;
        Node node = list;
        while (node.next != null) {
            node = node.next;
            i++;
            if (index == i) {
                return node.data;
            }
        }
        return null;
    }

    public static Number delete(Node<Number> list, int index) {
        Number elem = null;
        if (index < 0) {
            System.out.println("删除的位置错误");
            return null;
        } else {
            Node pre = list;
            int i = 0;
            Node node = null;
            while (pre.next != null) {
                i++;
                if (i == index) {
                    node = pre.next;
                    elem = node.data;
                    pre.next = node.next;
                    node.next = null;
                    return node.data;
                }
            }
            return elem;
        }
    }


    public static void main(String[] args) throws Exception {
        Node<Number> list = new Node<>();
        initList(list);
        print(list);
        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入你要插入元素的位置：");
        int index = scanner.nextInt();
        Number elem = NumberFormat.getInstance().parse(scanner.next());
        insert(list, index, elem);
        print(list);
        System.out.println("请输入你要查找的元素：");
        elem = NumberFormat.getInstance().parse(scanner.next());
        index = find(list, elem);
        System.out.println("您要找的元素" + elem + "的下标是：" + index);
        System.out.println("请输入你要删除元素的位置：");
        index = scanner.nextInt();
        elem = delete(list, index);
        System.out.println(index + "处删除的元素为：" + elem);
        print(list);
    }
//}
}



